package com.zmn.plat.admin.controller.orderconfig.distribute;

import com.zmn.account.common.utils.NumberUtil;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.common.exception.ParamOutOfRangeException;
import com.zmn.plat.common.exception.PlatException;
import com.zmn.plat.model.entity.orderconfig.distribute.DistributeWeightAreaVO;
import com.zmn.plat.model.entity.orderconfig.distribute.SearchCityQuery;
import com.zmn.plat.model.entity.orderconfig.distribute.UpdateStatusQuery;
import com.zmn.plat.model.entity.orderconfig.distribute.UploadCityConfigBO;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 类描述：派单参数配置页面ajax访问控制器
 *
 * @author Yangk
 * @version v1.0
 * @since 2021年1月29日
 */
@Slf4j
@RestController
@RequestMapping("/orderconfig/distribute/ajax/")
public class DistributeWeightAjaxController extends DistributeWeightBaseController {

    /**
     * 更新状态
     *
     * @return
     */
    @RequestMapping(value = "updateStatus", method = RequestMethod.POST)
    public ResponseDTO updateStatus(@RequestParam(value = "idList", defaultValue = "") String idList,
                                    @RequestParam(value = "status", defaultValue = "") Integer status) {
        UpdateStatusQuery updateStatusQuery = UpdateStatusQuery
                .builder()
                .idList(StringUtil.convertStrsToList(idList))
                .status(status)
                .updater(getMcStaffRealName())
                .updateTime(DateUtil.getNow())
                .build();
        try {
            distributeWeightPlatBService.updateStatus(updateStatusQuery);
            return ResponseDTO.success(null, "操作成功");
        } catch (ParamOutOfRangeException paramEx) {
            log.error("#DistributeWeightBaseController#uploadCityConfig# update failed! reason: {}", paramEx.getMessage());
            return ResponseDTO.fail("参数异常");
        } catch (Exception ex) {
            log.error("#DistributeWeightBaseController#uploadCityConfig# update failed! reason: {}", ex.getMessage());
            return ResponseDTO.fail("未知异常");
        }
    }

    /**
     * 根据省级ID 获取城市列表
     *
     * @param provinceId
     * @return
     */
    @RequestMapping(value = "searchCity", method = RequestMethod.POST)
    public ResponseDTO<List<DistributeWeightAreaVO>> searchCityByProvince(@RequestParam(value = "provinceId", defaultValue = "") Integer provinceId,
                                                                          @RequestParam(value = "distributePlatId", defaultValue = "") Integer distributePlatId,
                                                                          @RequestParam(value = "bizType", required = true) Integer bizType,
                                                                          @RequestParam(value = "plat", required = true) Integer plat) {

        try {
            SearchCityQuery searchCityQuery = SearchCityQuery.builder()
                    .distributePlatId(distributePlatId)
                    .provinceId(provinceId)
                    .bizType(bizType)
                    .plat(plat)
                    .build();
            List<DistributeWeightAreaVO> distributeWeightAreaVOList = distributeWeightPlatBService.listCityByProvince(searchCityQuery);
            return ResponseDTO.success(distributeWeightAreaVOList, "操作成功");
        } catch (ParamOutOfRangeException paramEx) {
            log.error("#DistributeWeightBaseController#searchCityByProvince# search failed! reason: {}", paramEx.getMessage(), paramEx);
            return ResponseDTO.fail("参数异常");
        } catch (Exception ex) {
            log.error("#DistributeWeightBaseController#searchCityByProvince# search failed! reason: {}", ex.getMessage(), ex);
            return ResponseDTO.fail("未知异常");
        }

    }

}
